#include "stdafx.h"
#include "ReactorDef.h"
#include "Selector.h"
#include "Validator.h"
#include "Affection.h"
#include "Utility.h"
#include "CombatDef.h"
#include "EventConditon.h"
#include "ReactorTypeDef.h"

struct  ReactorDefReflectionRegister
{
	ReactorDefReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iID,0).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_sName,std::string("")).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iTypeID,0).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_ReactorTypes);
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iLvel,1).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_eMatchEventID,eECombatEvent_DoCastSkill).Editor_SetAsEnumation<ECombatEvent>("Event");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iValidatorID,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_Validators,"Event");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iEventConditonID,INVALID_ID).Editor_SetAsForeignKeyReferenced(eDefGroup_EventConditons,"m_eMatchEventID","Event");

		REGISTER_REFLECTION_MEMBER(ReactorDef,m_bSeries,false).Editor_SetAsRaw("Affections");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iAffection1,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_Affections,"Affections");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iAffection2,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_Affections,"Affections");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iAffection3,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_Affections,"Affections");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iAffection4,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_Affections,"Affections");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iAffection5,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_Affections,"Affections");


		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iLifeTurn,INVALID_ID).Editor_SetAsRaw("Buff Option");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_bAsBuff,false).Editor_SetAsRaw("Buff Option");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iBuffRes,0).Editor_SetAsRaw("Buff Option");
		
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iHpMaxAdd,0).Editor_SetAsRaw("AddOn");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_fHpAddPercent,0.f).Editor_SetAsRaw("AddOn");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iAtkAdd,0).Editor_SetAsRaw("AddOn");		
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_fAtkAddPercent,0.f).Editor_SetAsRaw("AddOn");

		REGISTER_REFLECTION_MEMBER(ReactorDef,m_vExcludeTypes,std::vector<int32>()).Editor_SetAsForeignKeySet(eDefGroup_Default,eDefaultDef_ReactorTypes,"AddRule");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_bExludeSameType,false).Editor_SetAsRaw("AddRule");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_bExludeSameSender,false).Editor_SetAsRaw("AddRule");
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_bExludeCampeLvl,false).Editor_SetAsRaw("AddRule");

		REGISTER_REFLECTION_MEMBER(ReactorDef,m_sDesc,std::string("")).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iExpToNextLvl,100).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ReactorDef,m_iCostHn,1000).Editor_SetAsRaw();

		GlobalClassManager::Instance().RegisterClass(ReactorDef::s_GetReflection(),eDefaultDef_Reactors);
		GlobalDefManager::Instance().RegisterDefManger(&ReactorDefManager::Instance());

	}
}__reactordefreflectionregister;



rewrite  void ReactorDef::OnArchieveReaded()
{
	m_pValidator=ValidatorManager::Instance().LookupItem(m_iValidatorID);

	do
	{
		DefMangerBase* pDefManager=GlobalDefManager::Instance().LookupDefManager(eDefGroup_EventConditons,m_eMatchEventID);
		if(!pDefManager) break;
		m_pEventConditon=dynamic_cast<EventConditionBase*>(pDefManager->LookupReflectionItem(m_iEventConditonID));
	}while(false);

	m_vAffections.clear();
	const Affection * pAffection1=AffectionManager::Instance().LookupItem(m_iAffection1);
	const Affection * pAffection2=AffectionManager::Instance().LookupItem(m_iAffection2);
	const Affection * pAffection3=AffectionManager::Instance().LookupItem(m_iAffection3);
	const Affection * pAffection4=AffectionManager::Instance().LookupItem(m_iAffection5);
	const Affection * pAffection5=AffectionManager::Instance().LookupItem(m_iAffection5);
	if(pAffection1) m_vAffections.push_back(pAffection1);
	if(pAffection2) m_vAffections.push_back(pAffection2);
	if(pAffection3) m_vAffections.push_back(pAffection3);
	if(pAffection4) m_vAffections.push_back(pAffection4);
	if(pAffection5) m_vAffections.push_back(pAffection5);

	m_pType=ReactorTypeDefManager::Instance().LookupItem(m_iTypeID);
}

#define MAKE_REACTOR_TYPE_LEVEL_ID(type,level) (1000*type+level)
const ReactorDef * ReactorDefManager::LookupReactorDefByLvl( int32 iType,int32 iLevel ) const
{
	std::map<int32,const ReactorDef *>::const_iterator it= m_mIndexs.find(MAKE_REACTOR_TYPE_LEVEL_ID(iType,iLevel));
	if(it==m_mIndexs.end()) return NULL;
	return it->second;
}

void ReactorDefManager::OnArchieveReaded()
{
	__ReactorDefManager::OnArchieveReaded();

	m_mIndexs.clear();
	std::vector<const ReactorDef*> vReactorDefs;
	Enumerate(vReactorDefs);
	for(uint32 ii=0;ii<vReactorDefs.size();ii++)
	{
		m_mIndexs[MAKE_REACTOR_TYPE_LEVEL_ID(vReactorDefs[ii]->m_iTypeID,vReactorDefs[ii]->m_iLvel)]=vReactorDefs[ii];
	}

}